package com.controller;

import com.service.StudentService;
import com.model.Student;
import com.utils.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * Created by lee on 2017/6/30.
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    //JSP文件的访问地址，实现对应的JSP视图页面跳转
    @RequestMapping(value="/addStudent")
    public ModelAndView toAddStudent(){
        ModelAndView modelAndView=new ModelAndView("addStudent");
        return modelAndView;
    }

    @RequestMapping(value="/editorStudent")
    public ModelAndView toEditorStudent(){
        ModelAndView modelAndView=new ModelAndView("editorStudent");
        return modelAndView;
    }

    @RequestMapping(value="/listStudent")
    public ModelAndView toListStudent(){
        ModelAndView modelAndView=new ModelAndView("listStudent");
        return modelAndView;
    }
    /**
    * 添加学生方法
    * request.getParameter（）接受添加值
    * 返回result
    */
    @RequestMapping(value="/add",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult addStudent(HttpServletRequest request) {
        JSONResult result;
        String studentName = request.getParameter("studentName");
        Integer studentAge = Integer.parseInt(request.getParameter("studentAge"));
        String studentCity = request.getParameter("studentCity");
        Student student = new Student();
        student.setName(studentName);
        student.setAge(studentAge);
        student.setCity(studentCity);
        if (studentService.addStudent(student)) {
            result = new JSONResult(student);
            result.setMessage("success");
        }
        else {
            result = new JSONResult();
            result.setMessage("error");
        }
        return result;
    }
    /**
    *删除学生方法
    * request.getParameter（）获取学生Id
    * 返回result
    */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteStudentById(HttpServletRequest request) {
        JSONResult result = new JSONResult();
        Integer studentId = Integer.parseInt(request.getParameter("studentId"));
        if (!studentService.deleteStudentById(studentId))
            result.setMessage("error");
        return result;
    }
    /**
    * 修改学生方法
    * request.getParameter（）获取修改后学生信息
    * 返回result
    */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateStudent(HttpServletRequest request) {
        JSONResult result;
        Integer studentId= Integer.parseInt(request.getParameter("studentId"));
        String studentName = request.getParameter("studentName");
        Integer studentAge =Integer.parseInt(request.getParameter("studentAge"));
        String studentCity = request.getParameter("studentCity");
        Student student = new Student();
        student.setId(studentId);
        student.setName(studentName);
        student.setAge(studentAge);
        student.setCity(studentCity);
        if (studentService.updateStudent(student)) {
            result = new JSONResult(student);
            result.setMessage("success");
        }
        else {
            result = new JSONResult();
            result.setMessage("error");
        }
        return result;
    }
    /**
    *通过学生Id查找学生方法
    * request.getParameter()获取输入学生Id
    * 返回result
    */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult findStudentById(HttpServletRequest request) {
        JSONResult result;
        Integer studentId= Integer.parseInt(request.getParameter("studentId"));
        Student student = studentService.findStudentById(studentId);
        result = new JSONResult(student);
        result.setMessage("success");
        return result;
    }
    /**
     * 列出所有学生方法
     * */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult findAllStudent(){
        List<Student> list = studentService.findAllStudent();
        return new JSONResult(list);
    }
}
